/**活动列表 */
import { Space, Table } from 'antd';
import { useHistory } from 'umi';
import type { ColumnsType } from 'antd/lib/table';

const tableStatus: any = {
  '0': '全部',
  '1': '待审核',
  '2': '进行中',
  '3': '未开始',
  '4': '已驳回',
  '5': '已结束',
};
const tableAction: any = {
  '1': (
    <>
      <a>详情</a>
    </>
  ),
  '2': (
    <>
      <a>详情</a>
      <a>关闭</a>
    </>
  ),
  '3': (
    <>
      <a>详情</a>
      <a>删除</a>
    </>
  ),
  '4': (
    <>
      <a>详情</a>
      <a>重新发布</a>
      <a>删除</a>
    </>
  ),
  '5': (
    <>
      <a>详情</a>
      <a>删除</a>
    </>
  ),
};

const ActivityTable = (props: {
  params: any;
  setParams: any;
  tableData: any;
}) => {
  const { params, setParams, tableData } = props;
  const history = useHistory();
  const id = (val: any) => {
    history.push(
      `/createActivity/addActivity?activityBasicId=${val.activityBasicId}`,
    );
  };

  const columns: ColumnsType<any> = [
    {
      title: '活动名称',
      dataIndex: 'activityName',
      key: 'activityName',
    },
    {
      title: '创建人',
      dataIndex: 'activityContent',
      key: 'activityContent',
    },
    {
      title: '创建时间',
      dataIndex: 'gmtCreated',
      key: 'gmtCreated',
    },
    {
      title: '活动时间',
      key: 'endDate',
      render: (_, { startDate, endDate }: any) => `${startDate} - ${endDate}`,
    },
    {
      title: '状态',
      key: 'activityStatus',
      dataIndex: 'activityStatus',
      render: (_, record: any) =>
        record.isDraft ? '草稿' : tableStatus[record.activityStatus],
    },
    {
      title: '操作',
      key: 'action',
      render: (_, record: any) => (
        <Space size="middle">
          {record.isDraft ? (
            <>
              <a onClick={() => id(record)}>编辑</a>
              <a>删除</a>
            </>
          ) : (
            tableAction[record.activityStatus]
          )}
        </Space>
      ),
    },
  ];
  const changPase = (page: number, pageSize: number) => {
    setParams((val: any) => ({ ...val, page, pageSize }));
  };

  return (
    <div>
      <Table
        rowKey={'id'}
        columns={columns}
        dataSource={tableData.rows}
        pagination={{
          current: params.page,
          total: tableData.total,
          showQuickJumper: true,
          showTotal: (total) =>
            `共 ${total} 条记录 第${params.page}/${Math.trunc(total / 10)}`,
          onChange: changPase,
        }}
      />
    </div>
  );
};
export default ActivityTable;
